package com.leetcode.No1793;

import org.junit.Test;

import java.util.*;

public class Solution3 {
    public int maximumScore(int[] nums, int k) {
        int result = 0;
        int n = nums.length;
        int i = k;
        int j = k;
        for (int y = nums[k]; ;) {
            while (i >= 0 && nums[i] >= y) {
                --i;
            }
            while (j < n && nums[j] >= y) {
                ++j;
            }
            result = Math.max(result, (j - i - 1) * y);
            if (i < 0 && j >= n) {
                break;
            }
            y = Math.max((i >= 0) ? nums[i] : -1, (j < n) ? nums[j] : -1);
        }
        return result;
    }

    @Test
    public void test() {
//        int[] nums = new int[]{1,4,3,7,4,5};
        int[] nums = new int[]{5,5,4,5,4,1,1,1};
        int k = 0;
        System.out.println(maximumScore(nums, k));
    }
}
